<template>
  <!-- 可以自定义样式 -->
  <my-search v-bind:bgColor="'#00BFFF'" v-bind:radius="'16'" v-on:click="gotoSearch"></my-search>
  <view>
    <view class="scroll-view-container">
      <!-- 左侧的滚动视图区域 -->
      <scroll-view class="left-scroll-view" scroll-y v-bind:style="{height: currentWindowHeight + 'px'}">
        <block v-for="(item, index) in cateList" v-bind:key="index">
          <view v-bind:class="['left-scroll-view-item', index == active? 'active': '']" v-on:click="activeChange(index)">{{item.cat_name}}</view>
        </block>
      </scroll-view>
      <!-- 右侧的滚动视图区域 -->
      <scroll-view class="right-scroll-view" scroll-y  v-bind:scroll-top="scrollTop" v-bind:style="{height: currentWindowHeight + 'px'}">
        <view class="cate-second" v-for="(itemsecond, indexsecond) in cateSecondList" v-bind:key="indexsecond">
          <view class="cate-second-title">
            / {{itemsecond.cat_name}} /
          </view>
          <!--三级分类-->
          <view class="cate-third-list">
            <view class="cate-third-item" v-for="(itemthird,indexthird) in itemsecond.children" v-bind:key="indexthird" v-on:click="gotoGoodsList(itemthird)">
              <image v-bind:src="itemthird.cat_icon" mode=""></image>
              <text>{{itemthird.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  import badgeMix from '@/mixins/tabbar-badge.js'
  export default {
    mixins: [badgeMix],
    data() {
      return {
        currentWindowHeight: 0,
        cateList: [],
        active: 0,   //当前选中项的索引, 默认第一项被选中
        cateSecondList: [],
        scrollTop: 0
      };
    },
    onLoad(){
      //50px是搜索组件的高度
      const res = uni.getSystemInfoSync().windowHeight - 50;
      this.currentWindowHeight = res;
      this.getCateList();
    },
    
    methods: {
      async getCateList(){
        const res = await uni.$http.get("/api/public/v1/categories");
        if(res.data.meta.status != 200){
          return uni.$showMessage;
        }
        console.log("categories: ", res);
        this.cateList = res.data.message;
        const secondList = res.data.message[0].children;
        
        secondList.forEach(item => {
          item.children.forEach(thirdItem => {            
            let ugoIndex = thirdItem.cat_icon.indexOf('api-ugo-dev');
            if(ugoIndex != -1){
              thirdItem.cat_icon = thirdItem.cat_icon.replace('api-ugo-dev', 'api-hmugo-web');
            }
          })
        })
        this.cateSecondList = secondList;
      },
      //选中项改变处理方法
      activeChange(i){
        this.active = i;
        const secondList = this.cateList[i].children;     
        console.log('secondList', secondList);
        secondList.forEach(item => {
          if(item.children != undefined){
            item.children.forEach(thirdItem => {
              let ugoIndex = thirdItem.cat_icon.indexOf('api-ugo-dev');
              if(ugoIndex != -1){
                thirdItem.cat_icon = thirdItem.cat_icon.replace('api-ugo-dev', 'api-hmugo-web');
              }
            })
          }          
        });
        console.log('secondList', secondList);
        this.cateSecondList = secondList;
        //需要改变scrollTop的值才能使滚动条回到顶部
        this.scrollTop = this.scrollTop == 0 ? 1 : 0;
      },
      
      gotoGoodsList(item){
        uni.navigateTo({
          url: '/subpkg/good_list/good_list?cid=' + item.cat_id
        })
      },
      
      gotoSearch(){
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }
    }
  }
</script>

<style lang="scss">
  .scroll-view-container{
    display: flex;
    .left-scroll-view{
      width: 120px;
      
      .left-scroll-view-item {
        background-color: #F7F7F7;
        line-height: 60px;
        text-align: center;
        font-size: 12px;
        
        &.active {
          background-color: #FFFFFF;
          position: relative;
          
          &::before {
            content: ' ';
            display: block;
            width: 3px;
            height: 30px;
            background-color: #C00000;
            position: absolute;
            top: 50%;
            left: 0;
            transform: translateY(-50%);
          }
        }
      }      
    }    
  }
  
  .cate-second-title{
    font-size: 12px;
    font-weight: bolder;
    text-align: center;
    padding: 15px 0;
  }
  
  .cate-third-list {
    display: flex;
    flex-wrap: wrap;
    .cate-third-item {
      width: 33.33%;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      margin-bottom: 10px;
      
      image {
        width: 60px;
        height: 60px;
      }
      
      text {
        font-size: 12px;
      }
    }
  }
  
</style>
